using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Toolkit.UI
{
    public class UIShakeEffect : MonoBehaviour, IPointerEnterHandler
    {
        [SerializeField] private RectTransform target;
        private Tween _tween;
        
        private void Start()
        {
            if (target == null)
                target = GetComponent<RectTransform>();
            _tween = PointerEnterTween();
        }

        private Tween PointerEnterTween()
        {
            Sequence sequence = DOTween.Sequence()
                .Join(target.DOShakeAnchorPos(0.2f, 1f))
                .Join(target.DOShakeScale(0.2f, 0.5f))
                .SetAutoKill(false)
                .SetUpdate(true)
                .Pause();
            return sequence;
        }

        private void OnDestroy()
        {
            _tween?.Kill();
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            _tween.Restart();
        }
    }
}